﻿#include "facade.h"
#include "ui_facade.h"

#include <QDebug>

Facade::Facade(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Facade)
{
    ui->setupUi(this);
    ui->mList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->mView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    cleanTable();
    ui->mText->appendPlainText("cord : " +QStringLiteral("伞绳"));
    ui->mText->appendPlainText("duct : " + QStringLiteral("胶管"));
    ui->mText->appendPlainText("band : " + QStringLiteral("吸汗带"));
}

Facade::~Facade()
{
    delete ui;
}

void Facade::on_btn_submit_clicked()
{
    name=ui->in_name->text().trimmed();
    if(name.isEmpty() || ui->in_num->text().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"),QMessageBox::Ok);
        return;
    }

    bool ok;
    num=ui->in_num->text().toInt(&ok);
    if(!ok)
        QMessageBox::warning(this,QStringLiteral("无效输入"),QStringLiteral("请输入有效数字！"),QMessageBox::Ok);
    else
    {
        insertItem(name,num);
        getMaterial(name);
        ui->in_name->clear();
        ui->in_num->clear();
    }
}

void Facade::cleanTable()
{
    QSqlQuery query;
  //  query.exec("TRUNCATE TABLE td_parts");
    query.exec("delete from td_parts");
}

void Facade::insertItem(const QString &name, const int &num)
{
    int row=ui->mList->rowCount();
    QTableWidgetItem *item_id=new QTableWidgetItem(QString::number(row+1));
    QTableWidgetItem *item_name=new QTableWidgetItem(name);
    QTableWidgetItem *item_num=new QTableWidgetItem(QString::number(num));

    ui->mList->insertRow(row);
    ui->mList->setItem(row,0,item_id);
    ui->mList->setItem(row,1,item_name);
    ui->mList->setItem(row,2,item_num);
}

void Facade::getMaterial(const QString &name)
{
    ui->mText->appendPlainText(QStringLiteral("【")  + name+ QStringLiteral("】") );
    QSqlQuery query;
    query.exec(QString("select mat_name,mat_num from t_assembly where name='%1'").arg(name));
    while(query.next())
    {
        mat_name=query.value(0).toString();
        mat_num=query.value(1).toDouble();
        mat_num *= num;

        insertRecord(mat_name,mat_num);
        ui->mText->appendPlainText(mat_name + " " + QString::number(mat_num));
    }
}

void Facade::insertRecord(const QString &name, const double &num)
{
    QSqlQuery query;
    query.exec(QString("insert into td_parts (%1) values (%2)").arg(name).arg(num));
}

void Facade::sumResult()
{
    QString value;
    QSqlQuery query;

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("pjz_2k")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(0,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("pjz_4k")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(1,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("pjz_4c")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(2,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("kq_45")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(3,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("kq_50")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(4,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("band")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(5,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("red_cord")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(6,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("red_duct")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(7,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("green_cord")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(8,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("green_duct")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(9,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("black_cord")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(10,0,item);
    }

    query.exec(QString("select sum(%1) from td_parts").arg(QStringLiteral("black_duct")));
    if(query.next())
    {
        value=QString::number(query.value(0).toDouble());
        QTableWidgetItem *item =new QTableWidgetItem(value);
        ui->mView->setItem(11,0,item);
    }
}

void Facade::on_btn_sum_clicked()
{
    sumResult();
}
